package views;

import bean.Admin;
import service.AdminService;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    JLabel lblNewLabel_2 = new JLabel("请输入密码");

    private JPasswordField passwordField;
    AdminService as=new AdminService();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LoginFrame frame = new LoginFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public LoginFrame() {
        lblNewLabel_2.setVisible(false);
        setTitle("登录系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 376, 436);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("登录系统");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 28));
        lblNewLabel.setBounds(137, 41, 112, 63);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("编号：");
        lblNewLabel_1.setBounds(48, 147, 54, 15);
        contentPane.add(lblNewLabel_1);

        textField = new JTextField();
        textField.setBounds(112, 144, 181, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel lblNewLabel_1_1 = new JLabel("密码：");
        lblNewLabel_1_1.setBounds(48, 195, 54, 15);
        contentPane.add(lblNewLabel_1_1);

        JButton btnNewButton = new JButton("登录");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                char[] password = passwordField.getPassword();
                if(password.length==0) {

                    lblNewLabel_2.setVisible(true);

                }
                else {
                    Admin a=new Admin();


                    a.setA_id(textField.getText());
                    a.setA_pwd(passwordField.getText());
                    Admin a2=new Admin();
                    a2=as.SelectAdmin(a);
                    if(a2.getA_id()==null) {
                        JOptionPane.showMessageDialog(null,"账号或密码错误","提示",JOptionPane.PLAIN_MESSAGE);
                    }else {
                        JOptionPane.showMessageDialog(null,"登录成功","提示",JOptionPane.PLAIN_MESSAGE);
                        Main am=new Main();
                        am.setVisible(true);
                        LoginFrame.this.dispose();
                    }
                }

            }
        });
        btnNewButton.setBounds(94, 315, 93, 23);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("取消");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnNewButton_1.setBounds(197, 315, 93, 23);
        contentPane.add(btnNewButton_1);

        lblNewLabel_2.setForeground(new Color(255, 0, 0));
        lblNewLabel_2.setBounds(112, 257, 100, 15);
        contentPane.add(lblNewLabel_2);

        passwordField = new JPasswordField();
        passwordField.setBounds(112, 192, 181, 21);
        contentPane.add(passwordField);
    }
}
